package com.qf.agro.service.facilities.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.agro.mapper.facilities.FacilitiesConfigMapper;
import com.qf.agro.pojo.facilities.FacilitiesConfig;
import com.qf.agro.service.facilities.FacilitiesConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FacilitiesConfigServiceImpl implements FacilitiesConfigService {

    @Autowired
    private FacilitiesConfigMapper facilitiesConfigMapper;

    @Override
    public Page<FacilitiesConfig> findPage(Integer pageNum, Integer pageSize, String name,String faciiltiesId) {
        //1.设置分页信息
        Page<FacilitiesConfig> page = new Page<>(pageNum,pageSize);
        //2.创建查询条件
        LambdaQueryWrapper<FacilitiesConfig> queryWrapper = new LambdaQueryWrapper<>();
        //3.设置条件
        queryWrapper.like(name != null,FacilitiesConfig::getFacilitiesName,name);
        queryWrapper.eq(StringUtils.isNotEmpty(faciiltiesId), FacilitiesConfig::getFacilitiesId, faciiltiesId);
        queryWrapper.orderByDesc(FacilitiesConfig::getId);
        //4.查询
        page = facilitiesConfigMapper.selectPage(page,queryWrapper);
        //5.返回
        return page;
    }

    @Override
    public void save(FacilitiesConfig facilitiesConfig) {
        facilitiesConfigMapper.insert(facilitiesConfig);
    }

    @Override
    public void update(FacilitiesConfig facilitiesConfig) {
        facilitiesConfigMapper.updateById(facilitiesConfig);
    }

    @Override
    public void delete(Long id) {
        facilitiesConfigMapper.deleteById(id);
    }

    @Override
    public FacilitiesConfig getFacilitiesId(Long facilitiesId) {
        LambdaQueryWrapper<FacilitiesConfig> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(FacilitiesConfig::getFacilitiesId,facilitiesId);
        return facilitiesConfigMapper.selectOne(lambdaQueryWrapper);
    }


}
